package com.xiaobai.service.user;

import com.sun.istack.internal.Nullable;
import com.xiaobai.dao.BaseDao;
import com.xiaobai.dao.user.UserDao;
import com.xiaobai.dao.user.UserDaoImpl;
import com.xiaobai.pojo.User;
import org.junit.Test;

import java.sql.Connection;
import java.sql.SQLException;


/**
 * @Author 小白学CS
 * @Version 1.0
 */
public class UserServiceImpl implements UserService {
    //业务层调取dao层，所以调取dao层
    private UserDao userDao;

    public UserServiceImpl() {
        //创建无参构造UserDaoImpl即可实例化userDao
        userDao = new UserDaoImpl();
    }

    @Override
    public User login(String userCode, String password) throws SQLException {
        Connection conn = null;
        User user = null;

        try {
            conn = BaseDao.getConnection();
            //通过业务层调用对应的具体的数据库操作
            user = userDao.getLoginUser(conn, userCode);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(conn, null, null);
        }
        return user;
    }

    /*junit单元测试*/
    @Test
    public void test() throws SQLException {
        UserServiceImpl userService = new UserServiceImpl();
        @Nullable User admin = null;
        admin = userService.login("admin", "1234567890");
        System.out.println(admin.getUserPassword());
    }
}

